package com.manor.main.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.flyco.tablayout.SlidingTabLayout;
import com.gyf.immersionbar.ImmersionBar;
import com.manor.common.http.HttpSignCallback;
import com.manor.common.utils.KLog;
import com.manor.dynamic.activity.DynamicPublishActivity;
import com.manor.main.R;
import com.manor.main.activity.CommentsAndLikesActivity;
import com.manor.main.fragment.community.CommunityPagerAdapter;
import com.manor.main.http.MainHttpUtil;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;


/**
 * 社区
 */
public class MainCommunityFragment extends Fragment implements ViewPager.OnPageChangeListener {


    ImageView ivIcon2;


    ImageView ivIcon1;


    RelativeLayout rl;
    private SlidingTabLayout mTabLayout;
    private ViewPager mViewPage;
    private CommunityPagerAdapter mPageAdapter;
    TextView tv_news;

    public static MainCommunityFragment newInstance() {
        MainCommunityFragment fragment = new MainCommunityFragment();
        return fragment;
    }
    private long firstPressedTime;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_main_community, container, false);

        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mTabLayout = view.findViewById(R.id.tab_layout);
        mViewPage = view.findViewById(R.id.view_page);
        tv_news = view.findViewById(R.id.tv_news);
        ivIcon1 = view.findViewById(R.id.iv_icon1);
        ivIcon1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //查看评论点赞
                startActivity(new Intent(getContext(), CommentsAndLikesActivity.class));
            }
        });
        ivIcon2 = view.findViewById(R.id.iv_icon2);
        ivIcon2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getContext(), DynamicPublishActivity.class));
            }
        });
        rl = view.findViewById(R.id.rl);
        rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (System.currentTimeMillis() - firstPressedTime < 2000) {
                    mPageAdapter.gettop(mPosition);
                } else {
                    firstPressedTime = System.currentTimeMillis();
                }

            }
        });
        setAdapter();
        ImmersionBar.with(this)
                .transparentStatusBar()
                .init();
    }

    private void setAdapter() {
        FragmentManager fragmentManager = getChildFragmentManager();
        mViewPage.setOffscreenPageLimit(1);
        mPageAdapter = new CommunityPagerAdapter(fragmentManager, getContext());
        mViewPage.setAdapter(mPageAdapter);
        mViewPage.setCurrentItem(0);
        mTabLayout.setViewPager(mViewPage);
        mViewPage.addOnPageChangeListener(this);

        mTabLayout.getTitleView(0).setTextSize(17f);

    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }
 int mPosition ;
    @Override
    public void onPageSelected(int position) {
        mPosition = position;
        if (position == 0) {
            mTabLayout.setTextsize(18f);

            mTabLayout.getTitleView(0).setTextColor(Color.parseColor("#ffffff"));
            mTabLayout.getTitleView(1).setTextColor(Color.parseColor("#596177"));
            mTabLayout.getTitleView(1).setTextSize(16f);

            mPageAdapter.store();
        } else if (position == 1) {
            mTabLayout.setTextsize(18f);
   
            mTabLayout.getTitleView(1).setTextColor(Color.parseColor("#ffffff"));
            mTabLayout.getTitleView(0).setTextColor(Color.parseColor("#596177"));
            mTabLayout.getTitleView(0).setTextSize(16f);

            mPageAdapter.store();

        }
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        KLog.e("tag", hidden+"");
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        KLog.e("tag", isVisibleToUser+"");
    }

    @Override
    public void onPause() {
        super.onPause();
        KLog.e("tag","////////////////////////////");
        mPageAdapter.store();
    }

    @Override
    public void onResume() {
        super.onResume();
        setTopMsgIconNumber();
    }



    /**
     * 设置顶部消息图标的颜色
     */
    private void setTopMsgIconNumber() {
        MainHttpUtil.getDynamicToal(new HttpSignCallback() {
            @Override
            public void onSuccess(int code, String msg, String info) {
                if (code == 0) {
                    JSONObject jsonObject = JSON.parseObject(info);
                    String total = jsonObject.getString("total");
                    int i = Integer.parseInt(total);
                    if (i > 0) {
                        //有动态
                     //   Glide.with(getContext()).load(R.mipmap.pic_news).into(ivIcon1);
                        tv_news.setText(total+"");
                        tv_news.setVisibility(View.VISIBLE);
                    } else {
                        tv_news.setVisibility(View.GONE);
                        //无动态
                     //  Glide.with(getContext()).load(R.mipmap.pic_news).into(ivIcon1);
                    }
                }
            }
        });
    }
}
